﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Zoo.WpfControls.Helper;

namespace Zoo.WpfControls
{
    /// <summary>
    /// SearchTextBox.xaml 的交互逻辑
    /// </summary>
    public partial class SearchTextBox : UserControl
    {
        public SearchTextBox()
        {
            InitializeComponent();
            this.Loaded += SearchTextBox_Loaded;
        }

        private void SearchTextBox_Loaded(object sender, RoutedEventArgs e)
        {
            txtSearch_TextChanged(null, null);
        }

        public string SearchText
        {
            get { return (string)GetValue(SearchTextProperty); }
            set { SetValue(SearchTextProperty, value); }
        }

        public static readonly DependencyProperty SearchTextProperty =
            DependencyProperty.Register("SearchText", typeof(string), typeof(SearchTextBox), new PropertyMetadata(SearchTextPropertyChanged));

        private static void SearchTextPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs arg)
        {
            SearchTextBox? control = obj as SearchTextBox;
            string Data = (string)arg.NewValue;

            if (control != null && Data != null)
            {
                control.txtSearch.Text = Data;
            }
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            var Text = this.txtSearch.Text;
            SearchText = Text;

            if (string.IsNullOrEmpty(Text))
            {
                this.btnDelete.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.btnDelete.Visibility = Visibility.Visible;
            }

            int TextLength = Text.Length;
            AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(txtSearch);

            if (adornerLayer != null)
            {
                if (TextLength == 0)
                {
                    TextBoxAdorner _adorner = new TextBoxAdorner(txtSearch, "Searching");
                    adornerLayer.Add(_adorner);
                }
                else
                {
                    Adorner[] adorners = adornerLayer.GetAdorners(txtSearch);
                    if (adorners != null)
                    {
                        foreach (var adorner in adorners)
                        {
                            adornerLayer.Remove(adorner);
                        }
                    }
                }
            }
        }

        private void btnDelete_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {  
            this.txtSearch.Text = "";
        }
    }
}
